package com.yantong.service.impl;

import com.yantong.mapper.ProductCentreMapper;
import com.yantong.mapper.ProductCommentMapper;
import com.yantong.pojo.MarketingGroup;
import com.yantong.pojo.ProductCentre;
import com.yantong.pojo.ProductComment;
import com.yantong.pojo.vo.LevelVo;
import com.yantong.pojo.vo.ProductCommentListVo;
import com.yantong.pojo.vo.ProductCommentVo;
import com.yantong.service.MarketingGroupService;
import com.yantong.service.ProductCommentService;
import com.yantong.utils.PageUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductCommentServiceImpl implements ProductCommentService {
	private static final Logger logger = LoggerFactory.getLogger(ProductCommentServiceImpl.class);

	@Autowired
	private ProductCommentMapper commentMapper;
	@Autowired
	private ProductCentreMapper pCentreMapper;
//	@Autowired
//	private MarketingSeckillService marketingSeckillService;
	@Autowired
	private MarketingGroupService marketingGroupService;

	@Override
	public PageUtils<ProductCommentListVo> list(int offset, int limit, Long centreId, Long teamId, int type) {
		PageUtils<ProductCommentListVo> pageUtils = new PageUtils<>();
		HashMap<String, Object> map = new HashMap<>();
		map.put("centreId", centreId);
		map.put("teamId", teamId);
		if (type == 1) {
			map.put("replay", "replay");
		}
		if (type == 2) {
			map.put("appendReplay", "appendReplay");
		}
		int total = commentMapper.countByWhere(map);
		if (total > 0) {
			pageUtils.setTotal(total);
			pageUtils.setRows(commentMapper.listByWhere(map, new RowBounds(offset, limit)));
		} else {
			pageUtils.setRows(new ArrayList<>());
		}
		return pageUtils;
	}

	@Override
	public int save(ProductComment obj) {
		if (obj == null) {
			return 0;
		}
		return commentMapper.insertSelective(obj);
	}

	@Override
	public ProductComment getByKey(Long id) {
		if (id == null) {
			return null;
		}
		ProductComment comment = commentMapper.selectByPrimaryKey(id);
		if (comment != null && comment.getType() != null && comment.getProductId() != 0) {
			if (comment.getType().equals(0)) {
				ProductCentre c = pCentreMapper.getByKey(Long.valueOf(comment.getProductId()));
				comment.setProductName(c.getTitle());
			} else if (comment.getType().equals(2)) {
				MarketingGroup marketingGroup = marketingGroupService.getMarketingGroupById(Long.valueOf(comment.getProductId()));
				comment.setProductName(marketingGroup.getProductName());
			} else if (comment.getType().equals(3)) {
				ProductCentre c = pCentreMapper.getByKey(Long.valueOf(comment.getProductId()));
				comment.setProductName(c.getTitle());
			}
		}
		return comment;
	}

	@Override
	public int update(ProductComment obj) {
		if (obj == null || obj.getId() == null) {
			return 0;
		}
		return commentMapper.updateByPrimaryKeySelective(obj);
	}

	@Override
	public int remove(Long id) {
		if (id == null) {
			return 0;
		}
		ProductComment obj = new ProductComment();
		obj.setId(id);
		obj.setDeleted("1");
		return commentMapper.updateByPrimaryKeySelective(obj);
	}

	@Override
	public ProductCommentVo getNewComentByProductId(Long productId) {

		return commentMapper.getNewComentByProductId(productId);
	}

	@Override
	public String countComentCountByProductId(Long productId) {

		return commentMapper.countComentCountByProductId(productId);
	}

	@Override
	public ProductComment addProductComment(ProductComment comment) {
		if (comment == null) {
			return null;
		}
		commentMapper.insertSelective(comment);

		return comment;
	}

	@Override
	public BigDecimal getCentreGrade(Map params) {
		return commentMapper.getCentreGrade(params);
	}

	@Override
	public List<ProductComment> queryList(Map params) {
		return commentMapper.listByWhere(params);
	}

	@Override
	public int queryTotal(Map params) {
		return commentMapper.countByWhere(params);
	}

	@Override
	public List<LevelVo> getCommentLevelCount(Map params) {
		return commentMapper.getCommentLevelCount(params);
	}
}
